package com.zwps.common.mybatis.context;

import java.util.HashMap;
import java.util.HashSet;

/**
 * 
 * @ClassName: ThreadLocalSkipDataPermissionContext
 * @Description:
 * @author lyh
 * @date 2023年11月17日 下午3:59:56
 *
 */
public class ThreadLocalSkipDataPermissionContext {

    private static ThreadLocal<HashMap<String, HashSet<String>>> SKIPS = new ThreadLocal<HashMap<String, HashSet<String>>>();

    public static void set(String tableName, String columnName) {
        HashMap<String, HashSet<String>> map = get();
        HashSet<String> set = map.get(tableName);
        if (set == null) {
            map.put(tableName, set = new HashSet<String>());
        }
        set.add(columnName);
    }

    public static void set(String tableName, HashSet<String> columnNames) {
        HashMap<String, HashSet<String>> map = get();
        map.put(tableName, columnNames);
    }

    public static HashMap<String, HashSet<String>> get() {
        HashMap<String, HashSet<String>> map = SKIPS.get();
        if (map == null) {
            synchronized (ThreadLocalSkipDataPermissionContext.class) {
                if ((map = SKIPS.get()) == null) {
                    map = new HashMap<String, HashSet<String>>();
                    SKIPS.set(map);
                }
                return map;
            }
        }
        return map;
    }

    public static boolean skip(String tableName, String columnName) {
        HashSet<String> set = get().get(tableName);
        if (set == null) {
            return false;
        }
        return set.contains(columnName);
    }

    public static void clear() {
        SKIPS.remove();
    }

    public static void clear(String tableName, String columnName) {
        HashSet<String> set = get().get(tableName);
        if (set == null) {
            return;
        }
        set.remove(columnName);
    }

    public static void clear(String tableName) {
        get().remove(tableName);
    }
}
